package com.applink.sell_proof;

import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.format.DateFormat;
import android.view.ViewGroup;
import android.widget.TextView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.applink.sell_proof.api.DataHelper;
import com.applink.sell_proof.api.DataResult;
import com.applink.sell_proof.constant.Constant;
import com.applink.sell_proof.constant.ItemStatus;
import com.applink.sell_proof.dialog.MyProgressDialog;
import com.applink.sell_proof.model.Project;
import com.applink.sell_proof.util.Utility;

public class DetailProject extends BaseProject implements Observer {

	// public static final String PROJECT_LOCALID = "projectlocalid";
	public static final String PROJECT_ID = "projectid";
	public static final String OPPORTUNITY_STATUS = "opportitystatus";

	private String mProjectId;
	// private String mProjectLocalId;

	private boolean mIsDraft = true;

	@Override
	protected void onCreate(Bundle saveInstanceState) {

		super.onCreate(saveInstanceState);
		setTitle("Review Opportunity");

		mProjectId = getIntent().getStringExtra(PROJECT_ID);
		System.out.println(">>>> project = " + mProjectId);
		// mProjectLocalId = getIntent().getStringExtra(PROJECT_LOCALID);

		String OpportunityStatus = getIntent().getStringExtra(
				OPPORTUNITY_STATUS);

		if (!OpportunityStatus.equals(ItemStatus.DRAFT)) {

			mIsDraft = false;
			Utility.disableTextView(InputType.TYPE_NULL, (ViewGroup) mLayout);
		}

		// DataHelper.getInstance().getOpportunityDetail(mOpportunityId);

		showloading();
		DataHelper.getInstance().addObserver(this);
		DataHelper.getInstance().getProjectDetail(mProjectId);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if (!mIsDraft) {
			return true;
		}

		MenuItem delete = menu.add(Menu.NONE, R.id.menu_delete_item,
				Menu.FLAG_APPEND_TO_GROUP, R.string.delete);

		delete.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case R.id.menu_delete_item:
			Constant.CHECK_DELETED_DRAFT = true;
			Project project = new Project();

			// set name
			String name = BaseProject.instantBaseProject.getOpportunityName();

			// set company
			String company = getCompanyName();

			project.setName(name);
			project.setAmount(getEstAmount());
			project.setCustomerCompanyName(company);

			// set date
			project.setDate(mDate);

			// call to server to store
			saveOpportunity(project);
			break;
		default:
			break;
		}
		return false;
	}

	@Override
	public void update(Observable arg0, Object data) {

		DataResult result = (DataResult) data;

		switch (result.getMethodName()) {
		case getOpportunityDetail:
			Project oppurtunity = (Project) result.getData();

			((TextView) mLayout.findViewById(R.id.end_customer_company))
					.setText(oppurtunity.getCustomerCompanyName());

			((TextView) mLayout.findViewById(R.id.opportunity_name))
					.setText(oppurtunity.getName());

			((TextView) mLayout.findViewById(R.id.total_estimated_amount))
					.setText(Integer.toString(oppurtunity.getAmount()));

			String date = DateFormat.getDateFormat(this).format(
					new Date(oppurtunity.getDate()));

			((TextView) mLayout.findViewById(R.id.expected_closing_date))
					.setText(date);

			mDate = oppurtunity.getDate();

			break;
		case deleteOpportunity:

			Intent intent = new Intent(mContext, AllProjectActivity.class);
			mContext.startActivity(intent);
			break;
		default:
			super.update(arg0, data);
			break;
		}

		hideloading();
	}

	@Override
	protected void createOpportunity(Project project) {
		updateOpportunity(project, ItemStatus.PENDING);
	}

	@Override
	protected void saveOpportunity(Project project) {
		updateOpportunity(project, ItemStatus.DRAFT);
	}

	private void updateOpportunity(Project project, String status) {

		showloading();

		Project custom = project;

		// assign the opportunity status again
		custom.setStatus(status);

		// set the opportunity
		custom.setId(mProjectId);

		// custom.setLocalId(mProjectLocalId);

		DataHelper.getInstance().updateProject(custom);
		MyProgressDialog.getInstance().show(this.getSupportFragmentManager(),
				null);
	}
}
